import { defineStore } from 'pinia'
import { shortenUrl, getUrlStats, getUrlList } from '../api/shortener'

export const useShortenerStore = defineStore('shortener', {
  state: () => ({
    urls: [],
    currentStats: null,
    loading: false,
    error: null
  }),

  actions: {
    async createShortUrl(url, expiresAt) {
      this.loading = true
      try {
        const result = await shortenUrl(url, expiresAt)
        this.urls.unshift(result)
        return result
      } catch (error) {
        this.error = error.message
        throw error
      } finally {
        this.loading = false
      }
    },

    async fetchUrlStats(shortCode) {
      this.loading = true
      try {
        const stats = await getUrlStats(shortCode)
        this.currentStats = stats
        return stats
      } catch (error) {
        this.error = error.message
        throw error
      } finally {
        this.loading = false
      }
    },

    async fetchUrlList() {
      this.loading = true
      try {
        const list = await getUrlList()
        this.urls = list
        return list
      } catch (error) {
        this.error = error.message
        throw error
      } finally {
        this.loading = false
      }
    }
  }
}) 